import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { ResData, Status } from 'src/response/res-data.interface';
import { IsNull, Repository } from 'typeorm';
import { CreateCustomFlowSubmitDto } from './dto/create-custom-flow-submit.dto';
import { UpdateCustomFlowSubmitDto } from './dto/update-custom-flow-submit.dto';
import { CustomFlowSubmit } from './entities/custom-flow-submit.entity';

@Injectable()
export class CustomFlowSubmitService {
  constructor(@InjectRepository(CustomFlowSubmit) private customFlowSubmitRepository: Repository<CustomFlowSubmit>) { }

  async create(createCustomFlowSubmitDto: CreateCustomFlowSubmitDto) {
    let resdata: ResData;
    try {
      const result = await this.customFlowSubmitRepository.save(createCustomFlowSubmitDto);
      resdata = {
        code: Status.SUCCESS,
        msg: '保存成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  findAll(querys) {
    return `This action returns all customFlowSubmit`;
  }

  async findOne(id: string) {
    let resdata: ResData;
    try {
      const result = await this.customFlowSubmitRepository.findOne(id);
      resdata = {
        code: Status.SUCCESS,
        msg: '查询成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  async findCurFlow(matterSubmitId: string) {
    const result = await this.customFlowSubmitRepository.findOne({
      relations: ["customFlowNode"],
      where: {
        customFormSubmitId: IsNull(),
        matterSubmitId: matterSubmitId
      },
      order: {
        sort: "ASC"
      }
    });
    return result;
  }

  async findCompleteFlow(matterSubmitId: string) {
    const result = await this.customFlowSubmitRepository.find({
      relations: ["customFlowNode"],
      where: {
        matterSubmitId: matterSubmitId
      },
      order: {
        sort: "ASC"
      }
    });
    return result;
  }

  async update(id: string, updateCustomFlowSubmitDto: UpdateCustomFlowSubmitDto) {
    let resdata: ResData;
    try {
      const result = await this.customFlowSubmitRepository.update(id, updateCustomFlowSubmitDto);
      resdata = {
        code: Status.SUCCESS,
        msg: '更新成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  remove(id: string) {
    return `This action removes a #${id} customFlowSubmit`;
  }
}
